package net.sxnic.ugr.user.action;

/**
 * 
 * Introduction：用户保存action类
 * 
 * @author 孙宇飞 create date : 2010-5-16
 * @author 吕盛槐 update date : 2011-9-26
 */
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sxnic.comm.utils.CommUtils;
import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.UgrConstant;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.ugr.dept.DeptManager;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.user.User;
import net.sxnic.ugr.user.UserManager;
import net.sxnic.util.MvcUtils;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 
 * Introduction：保存用户action类
 * 
 * @author 孙宇飞 create date : 2010-5-16
 */
@Controller
@RequestMapping(value = "/admin/ugr")
public class Save extends UgrActionSupport {

	private static Logger logger = LoggerFactory.getLogger(Save.class);

	@Autowired
	private UserManager userManager;

	@Autowired
	private DeptManager deptManager;

	//,
	@RequestMapping(value = "user_save.jspa", method = RequestMethod.POST)
	public String execute(String id,String role, int page ,RedirectAttributes model)
			throws Exception {

		User user = userManager.get(id);
		try {
			// 验证登陆用户是否为管理员
			User adminuser = userManager.getUserByUsername(AppContext
					.getUserName(request));

			isUserInRole(adminuser, Role.SYSTEM_ADMIN);
			
			if (user == null) {
				addActionError(getText("user.error.notFound"));
				return INPUT;
			}

			String tempCodes = "";			
			
			//利用反射方式把页面上User的属性放到从数据库中取出的User中
			user = (User)MvcUtils.getEntityFromRequest(request, "user.", user);

			// 部门
			// if (StringUtils.isBlank(user.getDeptCodes())) {
			// user.getDepts().clear();
			// tempCodes = "";
			// } else {
			//
			// Dept deptment = null;
			// deptment = deptManager.findByCode(user.getDeptCodes());
			//
			// user.getDepts().clear();
			// user.getDepts().add(deptment);
			//
			// }

			// 角色
			if (StringUtils.isBlank(role)) {
				user.getRoles().clear();
				tempCodes = "";
			} else {
				tempCodes = "";

				String[] roles = StringUtils.split(role, ",");
				user.getRoles().clear();
				userManager.update(user);
				Role r = null;
				for (String rr : roles) {
					r = authorizationManager.getRole(rr.trim());

					logger.debug("role id :" + r.getName() + " , user id :"
							+ user.getId());

					user.getRoles().add(r);
					tempCodes = tempCodes + r.getName() + ",";
				}
				// user.setRoleCodes(StringUtils.removeEnd(tempCodes, ","));
			}

			userManager.update(user);
			logger.debug("update user success!" + user.getId());

		} catch (Exception e) {
			e.printStackTrace();
		}

		// 重建userMap
		UgrConstant.USER_MAP = userManager.findUserMap();

		// log
		addLog("user : " + AppContext.getUserName(request) + " update user :"
				+ user.getId() + " success.");

		// 保存成功信息并转向
		model.addAttribute("msg", "saveSuccess");
		model.addAttribute("page", page);

		return "redirect:user_browse.jspa";
	}
}
